﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using ZhouYu.NewWorld.BLL.IService;
using ZhouYu.NewWorld.DAL;
using ZhouYu.NewWorld.Model;
using ZhouYu.NewWorld.ViewModel;

namespace ZhouYu.NewWorld.BLL.Service
{
    public class CashnumberService : BaseRepository<Cashnumber>, ICashnumberService
    {
        /// <summary>
        /// 分页多表查询
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public ResponseResult CashnumberInfo(int page, int limit)
        {
            ResponseResult responseResult = new ResponseResult();
            //创建实例            
            using (var db = new SyDbContext())
            {
                //两表连接查询
                var result = from a in db.Cashnumber
                             join b in db.Cashnumber_record
                             on a.Cash_Id equals b.Cash_Id
                             select new CashNumberInfo() //自定义实体类 包括需要展示的字段
                             {
                                 //赋值
                                 User_Id = a.User_Id,
                                 CashRecord_Id = b.CashRecord_Id,
                                 Cash_Id = b.Cash_Id,
                                 CashRecord_Money = b.CashRecord_Money,
                                 CashRecord_State = b.CashRecord_State,
                                 CashRecord_OperationMethod = b.CashRecord_OperationMethod,
                                 CashRecord_Channel = b.CashRecord_Channel,
                                 CashRecord_Effective = b.CashRecord_Effective,
                                 CashRecord_CreateTime = b.CashRecord_CreateTime
                             };
                //responseResult对象赋值
                responseResult.data = result.ToList().Skip((page - 1) * limit).Take(limit);
                responseResult.count = result.ToList().Count;
                responseResult.msg = string.Empty;
                responseResult.code = 0;
            }
            return responseResult;
        }

        /// <summary>
        /// 多表删除
        /// </summary>
        /// <param name="cashnumbers"></param>
        /// <returns></returns>
        public ResponseResult DeleteCash(int id)
        {
            ResponseResult result = new ResponseResult();
            Cashnumber cashnumber = new Cashnumber();
            cashnumber.Cash_Id = id;
            using (var db = new SyDbContext())
            {
                //开启事务
                using (var tran = db.Database.BeginTransaction())
                {
                    try
                    {
                        List<Cashnumber_record> cashnumbers = db.Cashnumber_record.Where(a => a.Cash_Id == id).ToList();
                        //删除现金账号
                        db.Entry(cashnumber).State = EntityState.Deleted;

                        //循环删除当前现金账号下的操作记录
                        for (int i = 0; i < cashnumbers.Count; i++)
                        {
                            //根据主键id获取单个实体
                            var entity = db.Cashnumber_record.Find(cashnumbers[i].CashRecord_Id);
                            //如果实体不为空则进行删除
                            if (entity != null)
                            {
                                //删除
                                db.Cashnumber_record.Remove(entity);
                            }
                        }
                        //保存
                        db.SaveChanges();
                        //提交
                        tran.Commit();
                        result.Success = true;
                    }
                    catch (Exception ex)
                    {
                        //回滚
                        tran.Rollback();
                        result.Success = false;
                    }
                }
            }
            return result;
        }

        /// <summary>
        /// 删除现金操作记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ResponseResult DeleteCashRecord(int id)
        {
            ResponseResult result = new ResponseResult();
            Cashnumber_record cashnumber_Record = new Cashnumber_record();
            cashnumber_Record.CashRecord_Id = id;
            using (var db = new SyDbContext())
            {
                try
                {
                    //删除现金账号
                    db.Entry(cashnumber_Record).State = EntityState.Deleted;
                    //保存
                    db.SaveChanges();
                    result.Success = true;
                }
                catch (Exception ex)
                {
                    result.Success = false;
                }
            }
            return result;
        }

        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="cashnumber_Record"></param>
        /// <returns></returns>
        public ResponseResult UpdateState(Cashnumber_record cashnumber_Record)
        {
            ResponseResult result = new ResponseResult();
            Cashnumber_record record = new Cashnumber_record();
            using (var db = new SyDbContext())
            {
                try
                {
                    //查询现金账号操作表
                    record = db.Cashnumber_record.SingleOrDefault(a => a.CashRecord_Id == cashnumber_Record.CashRecord_Id);
                    if (record.CashRecord_State == 1)
                    {
                        record.CashRecord_State = 0;
                    }
                    else
                    {
                        record.CashRecord_State = 1;
                    }

                    //保存
                    db.SaveChanges();
                    result.Success = true;
                }
                catch (Exception ex)
                {
                    result.Success = false;
                }
            }
            return result;
        }
    }
}
